#include <stdio.h>

#define N 16	

int mystrlen(const char *p);
char *mystrncpy(char *dest, const char *src, int n);
char *mystrncat(char *dest, const char *src, int n);
int mystrcmp(const char *s1, const char *s2);

int main(void)
{
	char str[N] = "hello"; // 数组中的每一个元素都是变量,字符串变量
	char *p = "good morning"; // 指针存储字符串常量的起始地址
	char str2[] = "abc";

	printf("mystrlen(str):%d\n", mystrlen(str));
	printf("copy:%s\n", mystrncpy(str, p, N - 1));
	mystrncat(str, "const char *", N - mystrlen(str) - 1);
	puts(str);

	printf("%ld\n", sizeof(str2));

	return 0;
}

int mystrlen(const char *p)
{
	int len = 0;

	while (*p) {
		len++;
		p++;
	}

	return len;
}

char *mystrncpy(char *dest, const char *src, int n)
{
	int i;
	
	for (i = 0; src[i] && i < n; i++) {
		dest[i] = src[i];
	}
	for (; i < n; i++) {
		dest[i] = '\0';
	}

	return dest;
}

char *mystrncat(char *dest, const char *src, int n)
{
	return mystrncpy(dest + mystrlen(dest), src, n);
}

int mystrcmp(const char *s1, const char *s2)
{
	while (*s1 == *s2 && *s1) {
		s1 ++;
		s2 ++;	
	}
	return *s1 - *s2;
}



